package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysUser;
import com.tsd.system.entity.SysUserGroup;
import com.tsd.system.service.SysUserGroupService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysUserGroup 控制层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysUserGroupController extends SystemBaseController {

    private final SysUserGroupService sysUserGroupService;

    @Autowired
    public SysUserGroupController(SysUserGroupService sysUserGroupService) {
        this.sysUserGroupService = sysUserGroupService;
    }

    @ApiOperation(value = "根据id获取用户组信息")
    @ResponseBody
    @RequestMapping(value = "/getSysUserGroupById.action", method = RequestMethod.POST)
    public HttpResult<SysUserGroup> getSysUserGroupById() {
        HttpResult<SysUserGroup> hr = new HttpResult<SysUserGroup>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = (Long) map.get("id");
            super.checkEmpty(id, "请提供参数id");
            SysUserGroup sysUserGroup = sysUserGroupService.getSysUserGroupById(id);
            hr.setData(sysUserGroup);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存用户组信息")
    @ResponseBody
    @RequestMapping(value = "/saveSysUserGroup.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysUserGroup() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser sysUser = super.getSysUser();
            SysUserGroup t = JSON.parseObject(JSON.toJSONString(map), SysUserGroup.class);
            sysUserGroupService.saveSysUserGroup(t, sysUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除用户组信息")
    @ResponseBody
    @RequestMapping(value = "/deleteSysUserGroup.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysUserGroup() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = (Long) map.get("id");
            super.checkEmpty(id, "请提供参数id");
            SysUser sysUser = super.getSysUser();
            sysUserGroupService.deleteSysUserGroup(id, sysUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "分页查询用户组信息")
    @ResponseBody
    @RequestMapping(value = "/findSysUserGroups.action", method = RequestMethod.POST)
    public HttpResult<List<SysUserGroup>> findSysUserGroups() {
        HttpResult<List<SysUserGroup>> hr = new HttpResult<List<SysUserGroup>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            //String searchValue = (String) map.get("searchValue");
            //if (!HlpUtils.isEmpty(searchValue)) {
            //	params.put("searchValue", "%" + searchValue + "%");
            //}
            PageBean page = new PageBean(map);
            List<SysUserGroup> list = sysUserGroupService.findSysUserGroups(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
